<?php

require_once('../includes/DB.class.php');
require_once('../includes/SmartyManager.class.php');
require_once('../includes/exceptions/AppExceptions.class.php');
require_once('../includes/Verifier.class.php');
require_once('../includes/SimpleTableExporter.class.php');

$smarty = SmartyManager::getSmarty();

try{

$_GET['keyname'] = trim($_GET['keyname']);

$smarty->assign('keyname', $_GET['keyname']);
$smarty->assign('stype', $_GET['stype']);

$filename = $_GET['stype'].'_'.date('YmdHis').'.xls';

$exp = new SimpleTableExporter($filename);
if($_GET['stype'] == 'filename'){
	$_GET['keyname'] = trim($_GET['keyname']);
	$query = "select *
			from little_main where filename like '%{$_GET['keyname']}%' order by filename, certificationNumber";
		DB::query($query);
		
		
		$fileId = null;
	
	$certificationNumber = array();

	$certificationCodification = array();
	
	$sequenceNumber = array();
	 
	$manufactory = array();
	
	$authenticationMark = array();
	
	$steelNumber =  array();
	
	$size1 = array();
	
	$size2 = array();
	
	$size3 = array();
	
	$count = array();
	
	$unitWeight = array();
	
	$weight = array();
	
	$orderNumber = array();
	
	$orderSubitemNumber = array();
	
	$unitPrice = array();
	
	$materialNumber = array();
	
	$consignmentBatch = array();
	
	$sampleBatch = array();
	
	$facheNumber = array();
	
	$purchaseNumber = array();
	
	$shipmentDate = array();
	
	$month = array();
	
	$vehicleNumber = array();
	
	$consignee = array();
	
	$remark = array();
	
	$filename = array();
	
	$uploadTime = array();
		
		$result = DB::getResult();
		
		while($row = $result->fetch_assoc()){
			array_push($certificationNumber, $row['certificationNumber']);
			array_push($certificationCodification, $row['certificationCodification']);
			array_push($sequenceNumber, $row['sequenceNumber']);
			array_push($manufactory, $row['manufactory']);
			array_push($authenticationMark, $row['authenticationMark']);
			array_push($steelNumber, $row['steelNumber']);	
			array_push($size1, $row['size1']);
			array_push($size2, $row['size2']);
			array_push($size3, $row['size3']);
			array_push($count, $row['count']);
			array_push($unitWeight, $row['unitWeight']);	
			array_push($weight, $row['unitWeight']*$row['count']);
			array_push($orderNumber, $row['orderNumber']);
			array_push($orderSubitemNumber, $row['orderSubitemNumber']);
			array_push($unitPrice,$row['unitPrice']);
			array_push($materialNumber, $row['materialNumber']);
			array_push($consignmentBatch, $row['consignmentBatch']);
			array_push($sampleBatch,$row['sampleBatch']);
			array_push($facheNumber,$row['facheNumber']);
			array_push($purchaseNumber,$row['purchaseNumber']);
			array_push($shipmentDate,$row['shipmentDate']);
			array_push($month,$row['month']);
			array_push($vehicleNumber, $row['vehicleNumber']);
			array_push($consignee, $row['consignee']);
			array_push($remark, $row['remark']);
			array_push($filename, $row['filename']);
			array_push($uploadTime, $row['uploadTime']);
		}
		
	$data = array($certificationNumber,$certificationCodification, $sequenceNumber, 
				  $manufactory, $authenticationMark, $steelNumber, $size1, $size2, $size3, $count,
				  $unitWeight, $weight, $orderNumber, $orderSubitemNumber, $unitPrice,$materialNumber, 
				  $consignmentBatch, $sampleBatch,$facheNumber,$purchaseNumber,$shipmentDate,
				  $month,$vehicleNumber, $consignee, $remark, $filename, $uploadTime);
	$exp->insertMainData($data);
				
	$query = "select *
	from little_main2 where filename like '%{$_GET['keyname']}%' order by filename, certificationNumber";
	DB::query($query);
	
	
	$fileId = null;
	
	$certificationNumber = array();

	$count = array();
	
	$remainCount = array();
	
	$orderNumber = array();
	
	$orderSubitemNumber = array();
	
	$sampleBatch = array();

	$filename = array();
	
	$uploadTime = array();
		
	$result = DB::getResult();
	
	while($row = $result->fetch_assoc()){
		array_push($certificationNumber, $row['certificationNumber']);
		array_push($count, $row['count']);
		array_push($remainCount, $row['remainCount']);
		array_push($orderNumber, $row['orderNumber']);
		array_push($orderSubitemNumber, $row['orderSubitemNumber']);
		array_push($sampleBatch,$row['sampleBatch']);
		array_push($filename, $row['filename']);
		array_push($uploadTime, $row['uploadTime']);
	}
	
	$data = array($certificationNumber, $count, $remainCount,
			  $orderNumber, $orderSubitemNumber, $sampleBatch, $filename, $uploadTime);
	$exp->insertMain2Data($data);
				
	$query = "select *
	from little_main3 where filename like '%{$_GET['keyname']}%' order by filename, materialCode";
	DB::query($query);

	$materialCode = array();
	
	$sequenceNumber = array();
	 
	$manufactory = array();
	
	$authenticationMark = array();
	
	$steelNumber =  array();
	
	$size1 = array();
	
	$size2 = array();
	
	$size3 = array();
	
	$count = array();
	
	$unitWeight = array();
	
	$orderNumber = array();
	
	$orderSubitemNumber = array();
	
	$unitPrice = array();

	$purchaseNumber = array();
	
	$remark = array();
	
	$filename = array();
	
	$uploadTime = array();
	
	$result = DB::getResult();
	
	while($row = $result->fetch_assoc()){
		array_push($materialCode, $row['materialCode']);
		array_push($sequenceNumber, $row['sequenceNumber']);
		array_push($manufactory, $row['manufactory']);
		array_push($authenticationMark, $row['authenticationMark']);
		array_push($steelNumber, $row['steelNumber']);	
		array_push($size1, $row['size1']);
		array_push($size2, $row['size2']);
		array_push($size3, $row['size3']);
		array_push($count, $row['count']);
		array_push($unitWeight, $row['unitWeight']);			
		array_push($orderNumber, $row['orderNumber']);
		array_push($orderSubitemNumber, $row['orderSubitemNumber']);
		array_push($unitPrice,$row['unitPrice']);
		array_push($purchaseNumber,$row['purchaseNumber']);
		array_push($remark, $row['remark']);
		array_push($filename, $row['filename']);
		array_push($uploadTime, $row['uploadTime']);
	}
	$data = array($materialCode,$sequenceNumber,$manufactory,$authenticationMark,$steelNumber,
				  $size1,$size2,$size3,$count,$unitWeight,$orderNumber,$orderSubitemNumber,$unitPrice,
				  $purchaseNumber,$purchaseNumber,$remark,$filename,$uploadTime);
	$exp->insertMain3Data($data);
				
	$query = "select *
			from little_ruku where filename like '%{$_GET['keyname']}%' order by filename, certificationNumber";
		DB::query($query);
		$rukuDate = array();
		$certificationNumber = array();
		$certificationCodification = array();
		$consignmentBatch = array();
		$remark1 = array();
		$remark2 = array();
		$filename = array();
		$uploadTime = array();
		$result = DB::getResult();
		
		while($row = $result->fetch_assoc()){
			array_push($rukuDate, $row['rukuDate']);
			array_push($certificationNumber, $row['certificationNumber']);
			array_push($certificationCodification, $row['certificationCodification']);
			array_push($consignmentBatch, $row['consignmentBatch']);
			array_push($remark1, $row['remark1']);
			array_push($remark2, $row['remark2']);
			array_push($filename, $row['filename']);
			array_push($uploadTime, $row['uploadTime']);
		}
		
		$data = array($rukuDate, $certificationNumber, $certificationCodification, $consignmentBatch,
				$remark1, $remark2, $filename, $uploadTime);
		$exp->insertRukuData($data);

	$query = "select * from 
				(
					(select * from little_chuku where fileId in (select id from little_chuku_file where filename like '%{$_GET['keyname']}%')) as chukuTable
				  		left join
				  	(select id,filename,uploadTime from little_chuku_file) as chukuFileTable
				  	on (chukuFileTable.id = chukuTable.fileId)
				 )
				 order by certificationNumber";
		DB::query($query);
		
		$chukuDate = array();
		$certificationNumber = array();
		$certificationCodification = array();
		$handler = array();
		$consignmentBatch = array();
		$remark1 = array();
		$remark2 = array();
		$filename = array();
		$uploadTime = array();
		
		$result = DB::getResult();
		
		while($row = $result->fetch_assoc()){
			array_push($chukuDate, $row['chukuDate']);
			array_push($certificationNumber, $row['certificationNumber']);
			array_push($certificationCodification, $row['certificationCodification']);
			array_push($handler, $row['handler']);
			array_push($consignmentBatch, $row['consignmentBatch']);
			array_push($remark1, $row['remark1']);
			array_push($remark2, $row['remark2']);
			array_push($filename, $row['filename']);
			array_push($uploadTime, $row['uploadTime']);
		}
	
		$data = array($chukuDate, $certificationNumber, $certificationCodification, $handler, $consignmentBatch,
				$remark1, $remark2, $filename, $uploadTime);
		$exp->insertChukuData($data);
		$exp->export();
}

else if($_GET['stype']=='filename2'){
$_GET['keyname'] = trim($_GET['keyname']);
		
		$query = "select *
			from little_main where filename2 like '%{$_GET['keyname']}%' order by filename2, certificationNumber";
		DB::query($query);
		
		
		$fileId = null;
	
	$certificationNumber = array();

	$certificationCodification = array();
	
	$sequenceNumber = array();
	 
	$manufactory = array();
	
	$authenticationMark = array();
	
	$steelNumber =  array();
	
	$size1 = array();
	
	$size2 = array();
	
	$size3 = array();
	
	$count = array();
	
	$unitWeight = array();
	
	$orderNumber = array();
	
	$orderSubitemNumber = array();
	
	$unitPrice = array();
	
	$materialNumber = array();
	
	$consignmentBatch = array();
	
	$sampleBatch = array();
	
	$facheNumber = array();
	
	$purchaseNumber = array();
	
	$shipmentDate = array();
	
	$month = array();
	
	$vehicleNumber = array();
	
	$consignee = array();
	
	$remark = array();
	
	$filename = array();
	
	$uploadTime = array();
		
		$result = DB::getResult();
		
		while($row = $result->fetch_assoc()){
			array_push($certificationNumber, $row['certificationNumber']);
			array_push($certificationCodification, $row['certificationCodification']);
			array_push($sequenceNumber, $row['sequenceNumber']);
			array_push($manufactory, $row['manufactory']);
			array_push($authenticationMark, $row['authenticationMark']);
			array_push($steelNumber, $row['steelNumber']);	
			array_push($size1, $row['size1']);
			array_push($size2, $row['size2']);
			array_push($size3, $row['size3']);
			array_push($count, $row['count']);
			array_push($unitWeight, $row['unitWeight']);			
			array_push($orderNumber, $row['orderNumber']);
			array_push($orderSubitemNumber, $row['orderSubitemNumber']);
			array_push($unitPrice,$row['unitPrice']);
			array_push($materialNumber, $row['materialNumber']);
			array_push($consignmentBatch, $row['consignmentBatch']);
			array_push($sampleBatch,$row['sampleBatch']);
			array_push($facheNumber,$row['facheNumber']);
			array_push($purchaseNumber,$row['purchaseNumber']);
			array_push($shipmentDate,$row['shipmentDate']);
			array_push($month,$row['month']);
			array_push($vehicleNumber, $row['vehicleNumber']);
			array_push($consignee, $row['consignee']);
			array_push($remark, $row['remark']);
			array_push($filename, $row['filename2']);
			array_push($uploadTime, $row['uploadTime2']);
		}
	$data = array($certificationNumber,$certificationCodification, $sequenceNumber, 
				  $manufactory, $authenticationMark, $steelNumber, $size1, $size2, $size3, $count,
				  $unitWeight, $weight, $orderNumber, $orderSubitemNumber, $unitPrice,$materialNumber, 
				  $consignmentBatch, $sampleBatch,$facheNumber,$purchaseNumber,$shipmentDate,
				  $month,$vehicleNumber, $consignee, $remark, $filename, $uploadTime);
	$exp->insertMainData($data);
	$exp->export();
}
}catch(Exception $e){
	$smarty->assign('errMsg', $e);
	$smarty->assign('errTitle', '查询数据库时出现错误');
	$smarty->display('error.html');
	die();
}
?>